﻿namespace UnityEditor.Graphs.AnimationStateMachine
{
    using NUnit.Framework;
    using System;
    using System.Linq;
    using UnityEditor;
    using UnityEditor.Graphs;
    using UnityEditorInternal;
    using UnityEngine;

    internal class StateNode : UnityEditor.Graphs.AnimationStateMachine.Node
    {
        public UnityEditorInternal.State state;

        private void AddNewBlendTreeCallback()
        {
            BlendTree motion = this.state.GetMotion(AnimatorControllerTool.tool.motionSetIndex) as BlendTree;
            StateMachine layerStateMachine = AnimatorControllerTool.tool.animatorController.GetLayerStateMachine(AnimatorControllerTool.tool.selectedLayerIndex);
            bool flag = true;
            if (motion != null)
            {
                string title = "This will delete current BlendTree in state.";
                string message = "You cannot undo this action.";
                if (EditorUtility.DisplayDialog(title, message, "Delete", "Cancel"))
                {
                    MecanimUtilities.DestroyBlendTreeRecursive(motion);
                }
                else
                {
                    flag = false;
                }
            }
            else
            {
                Undo.RegisterUndo(layerStateMachine, "Blend Tree Added");
            }
            if (flag)
            {
                BlendTree objectToAdd = new BlendTree {
                    name = "Blend Tree"
                };
                objectToAdd.SetBlendEvent(AnimatorControllerTool.tool.animatorController.GetDefaultBlendTreeEvent());
                objectToAdd.hideFlags = HideFlags.HideInHierarchy;
                AssetDatabase.AddObjectToAsset(objectToAdd, layerStateMachine);
                this.state.SetMotion(AnimatorControllerTool.tool.motionSetIndex, objectToAdd);
            }
        }

        private void CopyStateCallback()
        {
            Unsupported.CopyStateToPasteboard(this.state);
        }

        private void MakeTransitionCallback()
        {
            base.graphGUI.edgeGUI.BeginSlotDragging(base.outputSlots.First<Slot>(), true, false);
        }

        public override void Move()
        {
            base.Move();
            this.state.SetPosition((Vector3) new Vector2(this.position.x, this.position.y));
        }

        internal override void NodeUI(UnityEditor.Graphs.GraphGUI host)
        {
            base.graphGUI = host as UnityEditor.Graphs.AnimationStateMachine.GraphGUI;
            Assert.NotNull(base.graphGUI);
            Event current = Event.current;
            if (UnityEditor.Graphs.AnimationStateMachine.Node.IsLeftClick())
            {
                host.edgeGUI.EndSlotDragging(base.inputSlots.First<Slot>(), true);
            }
            if (UnityEditor.Graphs.AnimationStateMachine.Node.IsDoubleClick())
            {
                Motion target = this.state.GetMotion(AnimatorControllerTool.tool.motionSetIndex);
                if (target is BlendTree)
                {
                    base.graphGUI.tool.AddBreadCrumb(target);
                }
                Selection.activeObject = target;
                current.Use();
            }
            if (UnityEditor.Graphs.AnimationStateMachine.Node.IsRightClick())
            {
                GenericMenu menu = new GenericMenu();
                menu.AddItem(new GUIContent("Make Transition"), false, new GenericMenu.MenuFunction(this.MakeTransitionCallback));
                if (base.graphGUI.rootStateMachine.DefaultState() == this.state)
                {
                    menu.AddDisabledItem(new GUIContent("Set As Default"));
                }
                else
                {
                    menu.AddItem(new GUIContent("Set As Default"), false, new GenericMenu.MenuFunction(this.SetDefaultCallback));
                }
                menu.AddItem(new GUIContent("Copy"), false, new GenericMenu.MenuFunction(this.CopyStateCallback));
                menu.AddItem(new GUIContent("Create new BlendTree in State"), false, new GenericMenu.MenuFunction(this.AddNewBlendTreeCallback));
                menu.ShowAsContext();
                current.Use();
            }
            Rect rect = GUILayoutUtility.GetRect((float) 200f, (float) 10f);
            if ((Event.current.type == EventType.Repaint) && ((base.graphGUI.liveLinkInfo.currentState == this.state) || (base.graphGUI.liveLinkInfo.nextState == this.state)))
            {
                GUIStyle style = "MeLivePlayBackground";
                GUIStyle style2 = "MeLivePlayBar";
                float num = (base.graphGUI.liveLinkInfo.currentState != this.state) ? base.graphGUI.liveLinkInfo.nextStateNormalizedTime : base.graphGUI.liveLinkInfo.currentStateNormalizedTime;
                rect = style.margin.Remove(rect);
                Rect position = style.padding.Remove(rect);
                position.width = (position.width * (num % 1f)) + 2f;
                style2.Draw(position, false, false, false, false);
                style.Draw(rect, false, false, false, false);
            }
        }

        private void SetDefaultCallback()
        {
            Undo.RegisterUndo(base.graphGUI.rootStateMachine, "Set Default State");
            base.graphGUI.rootStateMachine.SetDefaultState(this.state);
            AnimatorControllerTool.tool.RebuildGraph();
        }

        public override UnityEngine.Object selectionObject
        {
            get
            {
                return this.state;
            }
        }

        public override string title
        {
            get
            {
                return this.state.GetName();
            }
            set
            {
            }
        }

        public override UnityEngine.Object undoableObject
        {
            get
            {
                return this.state;
            }
        }
    }
}

